use super::EditorApp;
use eframe::egui;

impl EditorApp {
    pub fn show_situation_dialog(&mut self, ctx: &egui::Context) {
        let mut action_performed = false;
        let mut is_dialog_open = self.is_dialog_open;
        egui::Window::new("收藏局面")
            .open(&mut is_dialog_open)
            .show(ctx, |ui| {
                let id = self.session.current_situation;
                ui.add_space(10.0);
                ui.label(format!("局面ID：{}", id));
                ui.add_space(10.0);
                ui.horizontal(|ui| {
                    ui.label("局面名称：");
                    ui.text_edit_singleline(&mut self.situation_name);
                });
                ui.add_space(10.0);
                ui.vertical_centered(|ui| {
                    if ui.button("　确定　").clicked() {
                        self.session.book.update_situation_name(
                            id,
                            self.session.turn,
                            self.situation_name.clone(),
                        );
                        action_performed = true;
                    }
                });
                ui.add_space(10.0);
                ui.collapsing("已收藏的局面：", |ui| {
                    egui::ScrollArea::vertical().show(ui, |ui| {
                        for (id, turn, name) in self.session.book.named_situations.clone() {
                            if ui
                                .add(egui::Label::new(name).sense(egui::Sense::click()))
                                .clicked()
                            {
                                self.session.goto_situation(id, turn);
                            }
                        }
                    });
                });
                ui.add_space(10.0);
            });
        if action_performed || !is_dialog_open {
            self.is_dialog_open = false;
        }
    }
}
